package dobest.ray.com.jiongjiong.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;

import com.dobest.ray.corelibs.AppManager;
import com.dobest.ray.corelibs.http.RequestManager;
import com.dobest.ray.corelibs.utils.ToastMgr;
import com.dobest.ray.corelibs.views.LoadingDialog;

import cn.jpush.android.api.JPushInterface;
import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.views.NavigationBar;

public abstract class BaseActivity<T extends Presenter> extends AppCompatActivity {
    protected NavigationBar navi;
    public Handler mHandler;

    public abstract void findViews();

    public abstract void init();

    public abstract void addListeners();

    public LoadingDialog loadingDialog;

    protected T presenter;
    protected PresenterCallBack callBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setTitle("加载数据中");
        mHandler = new Handler();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        RequestManager.cancelPendingRequests(this);
        AppManager.getAppManager().finishActivity(this);
    }

    public void showToast(String s) {
        ToastMgr.show(s, 2);
    }

    public void showToast(int r) {
        ToastMgr.show(r, 2);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.in_left_right, R.anim.out_left_right);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_right_left, R.anim.out_right_left);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.in_right_left, R.anim.out_right_left);
    }

    /**
     * JPush需要在每个activity中做如此操作
     */
    @Override
    protected void onResume() {
		JPushInterface.onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
		JPushInterface.onPause(this);
        super.onPause();
    }

    public void setPresenter(T presenter, PresenterCallBack callBack) {
        this.presenter = presenter;
        this.callBack = callBack;
        presenter.setPresenterCallback(callBack);
    }

    public T getPresenter() {
        return presenter;
    }
}
